package com.dsg.controller.account;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.DateUtils;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccountLedger;
import com.dsg.service.account.IDsgAccountLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 油公司總帳明細 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@RestController
@RequestMapping("/account/dsgAccountLedger")
@Api(description = "油公司總帳明細")
public class DsgAccountLedgerController extends BaseController {

    @Autowired
    private IDsgAccountLedgerService dsgAccountLedgerService;

    /**
     * 查询列表（分页）
     *
     * @param dsgAccountLedger
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgAccountLedger dsgAccountLedger) {
        Page page = getPage();  // 分页
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            orderItemList.add(new OrderItem().setColumn("cashTime").setAsc(false));
            orderItemList.add(new OrderItem().setColumn("tranTime").setAsc(false));
            page.setOrders(orderItemList);
        }
        dsgAccountLedger.setEntCode(SingletonLoginUtils.getSystemEntCode());
        IPage<DsgAccountLedger> dsgAccountLedgerIPage = dsgAccountLedgerService.selectByList(page, dsgAccountLedger);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgAccountLedgerIPage.getRecords(), (int) page.getTotal()); // 格式要返回的数据
        return ajaxResult;
    }

    /**
     * 新增
     *
     * @param dsgAccountLedger
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgAccountLedger dsgAccountLedger) {
        dsgAccountLedgerService.addDsgAccountLedger(dsgAccountLedger);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    /**
     * 更新
     *
     * @param dsgAccountLedger
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgAccountLedger dsgAccountLedger) {
        AjaxResult ajaxResult;
        boolean flag = dsgAccountLedgerService.updateById(dsgAccountLedger);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgAccountLedger);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 兌現
     *
     * @param idList
     * @param cashTime
     * @return
     */
    @PostMapping(value = "/cashIn")
    @ApiOperation("兌現")
    public AjaxResult cashIn(@ApiParam(value = "id以，號分隔", name = "idList", required = true) @RequestParam String idList,
                             @ApiParam(value = "兌現時間", name = "cashTime", required = true) @RequestParam String cashTime) {
        DsgAccountLedger dsgAccountLedger = new DsgAccountLedger();
        dsgAccountLedger.setCashTime(DateUtils.parseDate(cashTime));
        dsgAccountLedger.setAccMode(DsgAccountLedger.AppStatus.already.getCode());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ACC_MODE", DsgAccountLedger.AppStatus.temporary.getCode());
        queryWrapper.in("OID", Arrays.asList(idList.split(",")));
        boolean flag = dsgAccountLedgerService.update(dsgAccountLedger, queryWrapper);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
}
